import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class TcpEchoClient {
	private Socket socket=null;

	public TcpEchoClient(String serverIp,int serverPort) throws IOException {
		socket=new Socket(serverIp,serverPort);
		//需要创建Socket对象的时候，和服务器建立连接，此时就得告诉Socket服务器在哪里
		//具体建立链接的细节，不需要咱们代码手动干预的，是内核自动负责的
		//当我们new这个对象的时候，操作系统内核，就开始进行三次握手 的具体细节了，
	}
	public void start() {
		Scanner scanner=new Scanner(System.in);
		try(InputStream inputStream=socket.getInputStream();
			OutputStream outputStream=socket.getOutputStream()) {
			PrintWriter writer=new PrintWriter(outputStream);
			Scanner scannerNetwork=new Scanner(inputStream);
			while(true) {
				System.out.println("-> ");
				String request=scanner.next();
				writer.println(request);
				writer.flush();
				String response =scannerNetwork.next();
				System.out.println(response);
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String[] args) throws IOException {
		TcpEchoClient tcpEchoClient=new TcpEchoClient("192.168.58.1",9090);
		tcpEchoClient.start();
	}
}
